<?php
namespace app\api\controller;
use app\model\Goods as GoodsModel;
use app\model\Token as TokenModel;
use think\Db;


class Goods extends Base
{   
    protected $noCheckToken = ['index','read'];

    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $map_params = [
            ['key'=>'category_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if ( isSearchParam('keyword_search') ) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        
        $map[] = ['status','=',1];

        // $order_type = 'sort desc,id desc';
        $order_mode = input('order_mode','id');
        $order_type = input('order_type','desc');

        $model = new GoodsModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order($order_mode,$order_type)->order('sort desc,id desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            if (!empty($vo['tags'])) {
                $list['data'][$k]['tags'] = explode(',',$vo['tags']);
            }else{
                $list['data'][$k]['tags'] = [];
            }

            if (!empty($vo['images'])) {
                $list['data'][$k]['images'] = explode(',',$vo['images']);
            }else{
                $list['data'][$k]['images'] = [];
            }

            $list['data'][$k]['params'] = json_decode($vo['params'],true);
            $list['data'][$k]['skus'] = json_decode($vo['skus'],true);
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * 获取详情
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function read($id){
        $user_id = $this->login_user['user_id'];
        $model = new GoodsModel();
        $data = $model->where('id',$id)->find();
        if (!empty($data)) {
            $model->where('id',$id)->inc('view_num')->update();
            $data['category_name'] = Db::name('category')->where('id',$data['category_id'])->value('name');

            if (!empty($data['tags'])) {
                $data['tags'] = explode(',',$data['tags']);
            }else{
                $data['tags'] = [];
            }

            if (!empty($data['images'])) {
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

            $data['params'] = json_decode($data['params'],true);
            $data['skus'] = json_decode($data['skus'],true);
        }
        
        return apiResult('2000','获取成功',$data);
    }

    /**
     * 点赞
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function like($id){
        $model = new GoodsModel();
        $model->where('id',$id)->inc('like_num')->update();
        
        return apiResult('2000','点赞成功');
    }

    public function recommend($id){
        $map_params = [
        ];
        $map = getSearchParam($map_params);
        $map[] = ['status','=',1];

        $model = new GoodsModel();
        $data = $model->where('id',$id)->find();
        $map[] = ['category_id','=',$data['category_id']];
        $map[] = ['id','<>',$data['id']];

        $list = $model->where($map)->order(Db::raw('RAND()'))->limit(3)->select();
        foreach ($list['data'] as $k => $vo) {
            if (!empty($vo['tags'])) {
                $list['data'][$k]['tags'] = explode(',',$vo['tags']);
            }else{
                $list['data'][$k]['tags'] = [];
            }

            if (!empty($vo['images'])) {
                $list['data'][$k]['images'] = explode(',',$vo['images']);
            }else{
                $list['data'][$k]['images'] = [];
            }
            
            $list['data'][$k]['params'] = json_decode($vo['params'],true);
            $list['data'][$k]['skus'] = json_decode($vo['skus'],true);
        }
        return apiResult('2000','获取成功',$list);
    }

}
